home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / GameKit / gamekit-1 / GKHighScorePanel.m < prev    next >
Text File  |  1995-06-12  |  7KB  |  175 lines

  1.  
  2. #import <gamekit/gamekit.h>
  3.  
  4.  
  5. @implementation GKHighScorePanel
  6.  
  7. - initContent:(const NXRect *)contentRect style:(int)aStyle
  8.         backing:(int)bufferingType buttonMask:(int)mask defer:(BOOL)flag;
  9. {
  10.     [super initContent:contentRect style:aStyle backing:bufferingType
  11.             buttonMask:mask defer:flag];
  12.     tableNum = 0;
  13.     numSlots = GK_HS_DEFAULT_SLOTS;    // assume that .nib is set up for this
  14.     wantSlot = GK_HS_NO_SLOT;
  15.     return self;
  16. }
  17.  
  18. - keyDown:(NXEvent *)myevent    // handle keyDown events (want to trap 'n')
  19. { // sent if no one else in the panel wants keys.
  20.     if (myevent->data.key.charSet == NX_ASCIISET &&
  21.         (myevent->flags&(NX_CONTROLMASK|NX_ALTERNATEMASK|NX_COMMANDMASK)) == 0)
  22.     {    
  23.         if (GKKEYCODE == 'n') { // allow new game
  24.             [[NXApp delegate] startNewGame:self]; // re-start the game
  25.             // returns nil if user changes mind...
  26.             return self; // so we don't beep, since we got the keypress
  27.         }
  28.     }
  29.     return [super keyDown:myevent];
  30. }
  31.  
  32. - setTable:(int)num { tableNum = num; return self; } // you have to refresh
  33. - changeTable:sender
  34. {    // sent by pop up list to display a new table;
  35.     // Note that which row you use determines the table's tag!
  36.     tableNum = [sender selectedRow]; // row should correspond to table number
  37.             // that the controller object is using.
  38.     [self refresh];
  39.     return self;
  40. }
  41.  
  42. - nameEntered:sender
  43. {    // sent when player enters their name; need to send name to delegate
  44.     id theCell;
  45.     if (wantSlot == GK_HS_NO_SLOT) return self;
  46.     theCell = [playerNameMatrix findCellWithTag:wantSlot];
  47.     if ([delegate respondsTo:@selector(nameForSlot:inTable:is:)]) 
  48.         [delegate nameForSlot:wantSlot inTable:wantTable
  49.                 is:[theCell stringValue]]; // if improper delegate, oh well.
  50.     [theCell setBackgroundGray:NX_LTGRAY];
  51.     [theCell setBezeled:NO];
  52.     [theCell setEditable:NO];
  53.     wantSlot = GK_HS_NO_SLOT;
  54.     return self;
  55. }
  56.  
  57. - ok:sender
  58. {    // ok button was hit, so enter the name and then get rid of the panel 
  59.     [self nameEntered:self];
  60.     [self orderOut:self];
  61.     return self;
  62. }
  63.  
  64. - resizeMatrixTo:(int)num
  65. {    // resize all matrices so that they have num cells.  This only works
  66.     // well if the matrices are in a ScrollView; if the numberMatrix is
  67.     // connected to something, then we assume that we're in a ScrollView
  68.     // since that matrix isn't needed otherwise.  Returns nil if can't
  69.     // resize *****This isn't working quite right.  Doesn't erase xtra junk!
  70.     // I've worked around it by forcing the table to be at least 10 slots
  71.     // in size.*****
  72.     int i;
  73.     char *tempString = malloc(num/10 + 2); // won't be any longer than this
  74.     if (num == numSlots) return self;    // we're already the right size
  75.     if (!numberMatrix) return nil;        // don't have a number matrix-no resize
  76.     if (num < 10) return nil;            // won't resize to smaller than 10
  77.     numSlots = num;
  78.     // add empty cells to all the matrices as needed
  79.     [numberMatrix notifyAncestorWhenFrameChanged:YES];
  80.     [[elapsedTimeMatrix renewRows:numSlots cols:1] sizeToCells];
  81.     [[endLevelMatrix renewRows:numSlots cols:1] sizeToCells];
  82.     [[endTimeMatrix renewRows:numSlots cols:1] sizeToCells];
  83.     [[finalScoreMatrix renewRows:numSlots cols:1] sizeToCells];
  84.     [[machineNameMatrix renewRows:numSlots cols:1] sizeToCells];
  85.     [[playerNameMatrix renewRows:numSlots cols:1] sizeToCells];
  86.     [[startLevelMatrix renewRows:numSlots cols:1] sizeToCells];
  87.     [[startTimeMatrix renewRows:numSlots cols:1] sizeToCells];
  88.     [[userNameMatrix renewRows:numSlots cols:1] sizeToCells];
  89.     [[numberMatrix renewRows:numSlots cols:1] sizeToCells];
  90.     for (i=1; i<=numSlots; i++) { // number the slots from 1 to numSlots
  91.         sprintf(tempString, "%d.", i); // note that value is copied by
  92.                 // the -setStringValue so that overwriting it is OK to do.
  93.         [[numberMatrix cellAt:0 :(i-1)] setStringValue:tempString];
  94.     }
  95.     return self;
  96. }
  97.  
  98. - refresh
  99. {    // get data from delegate and put it into the matrix and re-display
  100.     id theTable; int i, slotsInTable;
  101.     if ([delegate respondsTo:@selector(table:)])
  102.         theTable = [delegate table:tableNum];
  103.     else return self; // may as well bail if the delegate won't give us info!
  104.     slotsInTable = [theTable count];
  105.     // try to resize matrix to #slots in the table.
  106.     [[self disableFlushWindow] disableDisplay]; // hold off on the graphics...
  107.     [self resizeMatrixTo:slotsInTable];
  108.     for (i=0; i<numSlots; i++) { // now fill the matrices w/slot data
  109.         [[playerNameMatrix findCellWithTag:i]
  110.                 setStringValue:[[theTable objectAt:i] playerName]];
  111.         [[machineNameMatrix findCellWithTag:i]
  112.                 setStringValue:[[theTable objectAt:i] machineName]];
  113.         [[userNameMatrix findCellWithTag:i]
  114.                 setStringValue:[[theTable objectAt:i] userName]];
  115.                 
  116.         [[startTimeMatrix findCellWithTag:i]
  117.                 setStringValue:[[[theTable objectAt:i] startTime]
  118.                 stringValue]];
  119.         [[endTimeMatrix findCellWithTag:i]
  120.                 setStringValue:[[[theTable objectAt:i] endTime] stringValue]];
  121.         [[elapsedTimeMatrix findCellWithTag:i]
  122.                 setStringValue:[[[theTable objectAt:i] elapsedTime]
  123.                 stringValue]];
  124.         [[startLevelMatrix findCellWithTag:i]
  125.                 setIntValue:[[theTable objectAt:i] startLevel]];
  126.         [[endLevelMatrix findCellWithTag:i]
  127.                 setIntValue:[[theTable objectAt:i] endLevel]];
  128.         [[finalScoreMatrix findCellWithTag:i]
  129.                 setIntValue:[[theTable objectAt:i] finalScore]];
  130.     }
  131.     [[self reenableDisplay] displayIfNeeded]; // do all drawing at once
  132.     [[self reenableFlushWindow] flushWindowIfNeeded]; // flush it out once
  133.     return self;
  134. }
  135.  
  136. - getPlayerName:(int)index table:(int)table
  137. {    // sent by HighScoreController to allow player to enter name
  138.     id tempCell;
  139.     // if already looking to fill the name in a slot...
  140.     // user lost chance to enter name; send default name back to
  141.     // the delegate and then make it uneditable to allow next slot
  142.     // to be entered in by the user.  Done by telling self user just
  143.     // entered name, which, indirectly, has just happened.
  144.     [self nameEntered:self];
  145.     // now can set up to enter a new name.
  146.     if (table != tableNum) { // wants to put in a different table than the
  147.         // one currently displayed.
  148.         // so we update pop up list and then get the new table
  149.         // 1. select a new cell in the matrix and then
  150.         // 2. reset the title of the trigger button
  151.         [[[selectPopUp target] itemList] selectCellAt:tableNum :0]; // 1.
  152.         [selectPopUp setTitle:[[selectPopUp target] selectedItem]]; // 2.
  153.         tableNum = table; // set up to load in new table
  154.     }
  155.     [self refresh]; // with new slot in there, we need a refresh
  156.     wantSlot = index;
  157.     tempCell = [playerNameMatrix findCellWithTag:wantSlot];
  158.     [tempCell setBackgroundGray:NX_WHITE];
  159.     [tempCell setEditable:YES];
  160.     [tempCell setBezeled:YES];
  161.     // ***** users have suggested pausing before this part since there's an
  162.     // ***** annoying tendency to "keep playing" and thereby trashing the
  163.     // ***** name by hitting a key which comes here rather than to the game
  164.     // ***** view where the user thought it would go.  This is needs fixing.
  165.     // ***** maybe a complete event queue flush _and_ a short delay (such as
  166.     // ***** a kind of ending sequence would be in order.  I'm not so sure
  167.     // ***** that this object should worry about it, however.
  168.     [self makeKeyAndOrderFront:self];  // want user to enter name, so bring
  169.     [self makeFirstResponder:playerNameMatrix]; // up new first responder
  170.     [playerNameMatrix selectCell:tempCell]; // and select entry field
  171.     return self;
  172. }
  173.  
  174. @end
  175.